package com.dengdai.applibrary.network;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;

/**
 * @说明 网络工具
 * @作者 LY
 * @文件 NetWork.java
 * @时间 2015年6月12日 下午3:30:09
 * @版权 Copyright(c) 2015 LY-版权所有
 */
public class NetWork {
    /**
     * 网络状态
     *
     * @return boolean true网络连接正常
     */
    public static boolean getNetworkStatus(Context context) {
        ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (manager != null) {
            NetworkInfo networkinfo = manager.getActiveNetworkInfo();
            if (networkinfo == null || !networkinfo.isAvailable()) {
                return false;
            } else {
                return true;
            }
        }
        return false;
    }

    /**
     * 网络类型 是2g网络或者WIFI和3G的判断 (包含：wifi，2g,3g)
     *
     * @param context
     * @return 0是无网络 1是WIFI 2是2G 3是3G
     */
    public static String isWifiOr2GNetWork(Context context) {
        String flag = "";
        ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkinfo = manager.getActiveNetworkInfo();
        if (networkinfo != null && networkinfo.isAvailable()) {
            if (networkinfo.getType() == ConnectivityManager.TYPE_WIFI) {
                flag = "wifi";
            } else if (networkinfo != null && networkinfo.getType() == ConnectivityManager.TYPE_MOBILE) {
                // 电信2G是 NETWORK_TYPE_CDMA
                // 移动2G卡 NETWORK_TYPE_EDGE
                // 联通的2G NETWORK_TYPE_GPRS
                if (networkinfo.getSubtype() == TelephonyManager.NETWORK_TYPE_GPRS || networkinfo.getSubtype() == TelephonyManager.NETWORK_TYPE_CDMA || networkinfo.getSubtype() == TelephonyManager.NETWORK_TYPE_EDGE) {
                    flag = "2g";
                } else {
                    flag = "3g";
                }
            }
        } else {
            flag = "";
        }
        return flag;
    }

    /***
     * 网络类型
     *
     * @param context
     * @return 0.无网络 1.WIFI 2.手机网络2G 3.手机网络3G
     */
    public static int isWifiOr2GNetWorkType(Context context) {
        int flag = 0;
        ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkinfo = manager.getActiveNetworkInfo();
        if (networkinfo != null && networkinfo.isAvailable()) {
            if (networkinfo.getType() == ConnectivityManager.TYPE_WIFI) {
                flag = 1;
            } else if (networkinfo != null && networkinfo.getType() == ConnectivityManager.TYPE_MOBILE) {
                //电信2G是 NETWORK_TYPE_CDMA
                //移动2G卡 NETWORK_TYPE_EDGE
                //联通的2G NETWORK_TYPE_GPRS
                if (networkinfo.getSubtype() == TelephonyManager.NETWORK_TYPE_GPRS
                        || networkinfo.getSubtype() == TelephonyManager.NETWORK_TYPE_CDMA
                        || networkinfo.getSubtype() == TelephonyManager.NETWORK_TYPE_EDGE) {
                    flag = 2;
                } else {
                    flag = 3;
                }
            }
        } else {
            flag = 0;
        }
        return flag;
    }

    /**
     * 获取网络状态
     *
     * @param context
     * @return
     */
    public static String getWorkType(Context context) {
        int type = isWifiOr2GNetWorkType(context);
        if (type == 0) {
            return "无网络";
        } else if (type == 1) {
            return "WIFI";
        } else if (type == 2) {
            return "2G";
        } else if (type == 3) {
            return "3G";
        } else {
            return "4G";
        }
    }
}
